package speculoos.jndi;

import javax.naming.Name;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Simple implementation of the JNDIConnection, delegation all methods to the DirContext
 * @author trecloux
 */
public class SimpleJNDIConnectionImpl implements JNDIConnection {

	/** Delegated directory */
	protected DirContext _directory;
	/** Logger */
	private static final Log LOG = LogFactory.getLog(SimpleJNDIConnectionImpl.class);
	
	/** 
	 * Simple constructor
	 * @param dirc delegated DirContext
	 */
	public SimpleJNDIConnectionImpl(DirContext dirc) {
		_directory = dirc;
	}
	
	/**
	 * Getter on directory
	 * @return the directory
	 */
	public DirContext getDirectory() {
		return _directory;
	}



	/** {@inheritDoc} */
	public void createSubcontext(Name name, Attributes attrs)
			throws NamingException {
		_directory.createSubcontext(name, attrs);
	}

	/** {@inheritDoc} */
	public void destroySubcontext(Name name) throws NamingException {
		_directory.destroySubcontext(name);
	}

	/** {@inheritDoc} */
	public Attributes getAttributes(Name name, String[] returningAttributes)
			throws NamingException {
		return _directory.getAttributes(name, returningAttributes);
	}

	/** {@inheritDoc} */
	public String getNameInNamespace() throws NamingException {
		return _directory.getNameInNamespace();
	}

	/** {@inheritDoc} */
	public NameParser getNameParser(String directoryName) throws NamingException {
		return _directory.getNameParser(directoryName);
	}

	/** {@inheritDoc} */
	public void modifyAttributes(Name name, ModificationItem[] mods)
			throws NamingException {
		_directory.modifyAttributes(name, mods);

	}

	/** {@inheritDoc} */
	public void rename(Name name, Name newName) throws NamingException {
		_directory.rename(name, newName);

	}

	/** {@inheritDoc} */
	public NamingEnumeration search(Name name, String filter,
			SearchControls controls) throws NamingException {
		return _directory.search(name, filter, controls);
	}

	/** {@inheritDoc} */
	public void close() {
		try {
			_directory.close();
		} catch (NamingException e) {
			LOG.warn("Error in closing context", e);
		}
	}
}